﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using S.Common.helper;
using S.Initial.API.Application.Command.BreedingMothers;
using S.Initial.API.Application.CommandHandlers.BreedingMothers;
using S.Initial.Infrastructure.Dto;
using S.Transfer.Domain.Model;

namespace S.Initial.API.Controllers
{
    /// <summary>
    /// 期初控制器（增删改）
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class InitialController : ControllerBase
    {
        private readonly IMediator mediator;
        private readonly IConfiguration configuration;

        public InitialController(IMediator mediator, IConfiguration configuration)
        {
            this.mediator = mediator;
            this.configuration = configuration;
        }

        #region 期初
        /// <summary>
        /// 获取连接数据库字符串
        /// </summary>
        /// <param name="configuration"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        /// 
        [HttpGet]
        [Route("apollo")]
        public IActionResult ApolloTest([FromServices] IConfiguration configuration, string key)
        {
            return Content(configuration.GetValue<string>(key));
        }
        #endregion

        #region 期初批量添加
        /// <summary>
        /// 期初批量添加
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        //[Authorize]
        [HttpPost]
        public async Task<int> AddAllBreedingMothers(CreateBreedingMothersCommand t)
        {
            return await mediator.Send(t);

        }
        #endregion

        #region 批量删除
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        //[Authorize]
        [HttpPost]
        public async Task<int> DeletedAllInitial(DeletedAllinitialCommand t)
        {
            return await mediator.Send(t);
        }
        #endregion

        #region 删除栋舍
        /// <summary>
        /// 删除栋舍
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        //[Authorize]
        [HttpGet] 
        public IActionResult DeleteSheepTrans(string EarTag, DateTime PregnancyDate)
        {
                var jsonString = HttpClientHelper.DeleteResponse($"{configuration.GetValue<string>("Gateway")}/transset/api/TransFer/DeleteSheepTransFer?EarTag=" + EarTag + "&SurrenderDate=" + PregnancyDate.ToString("yyyy-MM-dd HH:mm:ss"));
                return Ok(jsonString);
        }
        /// <summary>
        /// 根据耳号查询所对应的全部信息
        /// </summary>
        /// <param name="EarTag"></param>
        /// <param name="PregnancyDate"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult DeleteSheepTransById(string EarTag, DateTime PregnancyDate)
        {
            SheepRescission reqult = HttpClientHelper.GetResponse<SheepRescission>($"{configuration.GetValue<string>("Gateway")}/transferset/api/SheepFrom/GetSheepFromByDate?EarTag={EarTag}");
            DateTime? ZhuanSheDate = reqult.SurrenderDate;
            if (ZhuanSheDate > PregnancyDate)
            {
                return Ok(0);
            }
            else
            {
                return Ok(1);
            }
        }
        #endregion

        #region 审核
        /// <summary>
        /// 审核
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        /// 
        //[Authorize]
        [HttpPost]
        public async Task<int> AuditBreeding(AuditBreeedingCommand t)
        {
            return await mediator.Send(t);
        }
        #endregion

        #region 修改
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        /// 
        //[Authorize]
        [HttpPost]
        public async Task<int> UpdateBreeding(UpdateBreedingMothersCommand t)
        {
            return await mediator.Send(t);
        }
        #endregion

        #region 读取配置文件
        /// <summary>
        /// 测试
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult TestGet()         
        {
            var cfRt = configuration.GetValue<string>("Consul");//读取Consul路由
            return Ok(cfRt);
        }
        #endregion

    }
}
